<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Object_name_model extends Base_model {

    protected $_table_name = 'object_name';
    protected $_key_name = 'object_name_id';

    function get_info($id) {
        if (!empty($id))
            $where[$this->_key_name] = $id;
        $where['dtime'] = 0;
        return $this->fetch(NULL, $where, NULL, 'row');
    }

    function get_all() {
        $this->set_order_by('sequence,ASC');
        $where = array('dtime' => 0);
        return $this->fetch(NULL, $where);
    }

    function get_not_in($i) {

        $v = explode(",", $i);
        $this->db->where_not_in('value', array_map('trim', $v));
        $this->set_order_by('sequence,ASC');
        $where = array('dtime' => 0);
        return $this->fetch(NULL, $where);
    }

    function get_building_name() {
        $this->set_order_by('sequence,ASC');
        $where = array('dtime' => 0);
        $result = $this->fetch(NULL, $where);
        $p = array();
        foreach ($result as $key => $val) {
            $p[$val->building_name_id] = $val->value;
        }
        return $p;
    }

    function get_demand_building_name($keyword) {
        if ($keyword != "")
            $this->db->like('value', $keyword, 'after');
        $user_purview = check_broker_is_visible();
        if (!$user_purview) {
            $this->set_or_like('front_man,broker', $this->login_lib->users_id());
        }
        $where['dtime'] = 0;
        $result = $this->fetch(NULL, $where);
        $p = array();
        foreach ($result as $key => $val) {
            $p[$val->building_name_id]["value"] = urlencode($val->value);
            $p[$val->building_name_id]["explanation"] = urlencode($val->explanation);
        }
        return $p;
    }

    function query_keyword($keyword = "", $limit) {
        $this->set_or_like('value,explanation', $keyword);
        $this->set_limit($limit);
        $where = array('dtime' => 0);
        return $this->fetch(NULL, $where);
    }

    function query_limit($num, $limit) {
        $query = $this->db->get('building_name', $num, $limit);
        return $query->result();
    }

    function query_keyword_count($keyword = null) {
        $this->set_or_like('value,explanation', $keyword);
        $con["where"][] = array('dtime' => 0);
        $this->set_args_array($con);
        return $this->fetch_count();
    }

    function update_data($data) {
        $value = array('value' => $data['value'], 'explanation' => $data['explanation'], 'sequence' => $data['sequence'], 'mtime' => time());
        $where[] = array($this->_key_name => $data['id']);
        return $this->update($value, $where);
    }

    function check_is_count($v) {
        $con["where"][] = array('value' => $v, 'dtime' => 0);
        $this->set_args_array($con);
        return $this->fetch_count();
    }

    function is_add($data) {
        $c = $this->check_is_count($data['value']);
        if ($c == 0) {
            $data['ctime'] = time();
            return $this->insert($data);
        }
    }

    function add($data) {
        $data['ctime'] = time();
        return $this->insert($data);
    }

    function del($data) {
        $where[] = array($this->_key_name => $data['id']);
        $value = array('dtime' => time());
        return $this->update($value, $where);
    }

}
